from django.shortcuts import render, redirect

from app01 import models
from app01.utils.pagination import Pagination
from app01.utils.form import AdminForm


def admin_list(request):
    """管理员列表"""
    # 搜索
    data_dict = {}
    cearch_data = request.GET.get('username',"")
    if cearch_data:
        data_dict['username__contains'] = cearch_data

    # 从数据库中获取数据
    queryset = models.Admin.objects.filter(**data_dict)

    # 分页
    page_object = Pagination(request,queryset,page_size=2)
    context = {
        "cearch_data": cearch_data,  # 筛选条件
        "queryset":page_object.page_queryset,
        "page_string":page_object.html()
    }
    return render(request,'admin.html',context)
False

def admin_add(request):
    title = '添加管理员'
    if request.method == 'GET':

        form = AdminForm()
        context  = {"form":form,"title":title}
        return render(request,'change_add.html',context)

    form = AdminForm(data = request.POST)

    if form.is_valid():
        form.save()
        return redirect('/admin/list')
    return render(request,"change_add.html",{"form":form})
